#include "stdafx.h"
#include "Form1.h"
#include "NewItemForm.h"
using namespace ItemMgr;



Form1::Form1(void)
		{
			InitializeComponent();
			m_ItemList = gcnew ManagedItemList;
			this->itemNameList->SelectionMode = SelectionMode::One;
			this->InspectBox->MouseClick += gcnew MouseEventHandler(this, &Form1::InspectBox_MouseClick);
			this->PickupBox->MouseClick += gcnew MouseEventHandler(this, &Form1::PickupBox_MouseClick);
			this->RelationshipBox->MouseClick += gcnew MouseEventHandler(this, &Form1::RelationshipBox_MouseClick);
			disableProperties();
		}

Void Form1::createNewButton_Click(System::Object^  sender, System::EventArgs^  e)
{
	NewItemForm^ newForm= gcnew NewItemForm(m_ItemList, this);
}

Void Form1::deleteButton_Click(System::Object^  sender, System::EventArgs^  e) 
{
	if(itemNameList->SelectedIndex == -1)
	{
		// insult user if no object is selected
		System::Windows::Forms::MessageBox::Show("You need to select an item to delete", "Jackass");
		return;
	}
	// remove object from vector, then remove name from list
	m_ItemList->remove_Item(itemNameList->SelectedIndex);
	itemNameList->Items->Remove(itemNameList->SelectedItem);

}

Void Form1::itemNameList_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e)
{

	
	// this function gets called after an item is deleted, so need to make sure index is valid
	if(itemNameList->SelectedIndex == -1)
	{
		// if not valid, clear box and fade out properties
		itemDescriptionBox->Clear();
		disableProperties();
		return;
	}
	m_currentItem = m_ItemList[itemNameList->SelectedIndex];
	//if valid, clear previous properties, and show correct properties
	RelationshipBox->CheckState = CheckState::Unchecked;
	PickupBox->CheckState = CheckState::Unchecked;
	InspectBox->CheckState = CheckState::Unchecked;

	itemDescriptionBox->Text = m_currentItem->getItemDescription();
	Byte currentFlags = m_currentItem->getItemFlags();
	if (currentFlags & ifRelationship)
		RelationshipBox->CheckState = CheckState::Checked;
	if (currentFlags & ifPickup)
		PickupBox->CheckState = CheckState::Checked;
	if (currentFlags & ifInspect)
		InspectBox->CheckState = CheckState::Checked;
	enableProperties();
}

Void Form1::ItemDescriptionBox_TextChanged(System::Object^  sender, System::EventArgs^  e)
{
	//gets called even after object is deleted, so need to make sure it's a valid index
	if(itemNameList->SelectedIndex != -1)
	{
		// just passes the text from the box, to the Item description
		m_ItemList[itemNameList->SelectedIndex]->setItemDescription(itemDescriptionBox->Text);
	}
}

Void Form1::PickupBox_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
{
	Byte temp = m_currentItem->getItemFlags();
	temp ^= ifPickup;
	m_currentItem->setItemFlags(temp);
}
Void Form1::InspectBox_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) 
{
	Byte temp = m_currentItem->getItemFlags();
	temp ^= ifInspect;
	m_currentItem->setItemFlags(temp);
}
Void Form1::RelationshipBox_MouseClick(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) 
{
	Byte temp = m_currentItem->getItemFlags();
	temp ^= ifRelationship;
	m_currentItem->setItemFlags(temp);
}